<!-- 编辑弹窗 -->
<template>
    <ele-modal
      inner
      width="80%"
      :movable="false"
      :visible="visible"
      :close-on-click-modal="true"
      :before-close="handleClose"
      custom-class="ele-dialog-form"
      :title="'销售出库明细'"
      @update:visible="updateVisible"
    >
    <div class="ele-body">
      <el-card>
        <salecommissionitemListSearch @search="reload"  />
      </el-card>
      <el-card>
        <ele-pro-table ref="table"  row-key="id" :selection.sync="selection"  :columns="warehouseItemColumns"  :datasource="datasource"  :need-page="true"   :height="tableHeight" :full-height="tableFullHeight"  @selection-change="handleSelectionChange">
          <template slot="toolbar">
          </template>
          <template slot="billNo" slot-scope="{ row }"  >
            <el-link  type="primary"   @click="clickbillNo(row)" >{{ row.billNo }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
    </div>
    <div slot="footer">
      <el-button type="primary" :loading="loading" @click="save" >
        确定
      </el-button>
    </div>
    </ele-modal>
  </template>
  <script>

  //初始化对像
  import { removePageTab } from '@/utils/page-tab-util';
  import salecommissionitemListSearch from './salecommissionitem-list-search.vue';
  import { QuerySaleoutorderByCondition } from '@/api/businesshandling/saleoutmanger';
  import { AddPaysalecommission } from '@/api/finance/salecommission';

  export default {
    name: 'SalecommissionItemList',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    components:{salecommissionitemListSearch},
    data() {
      return {
        // 表单数据
        form: {},
        fixedHeight:true,
        // 提交状态
        loading: false,
        current:null,
        // 是否是修改
        isUpdate: false,
        showEdit:false,
        selectRows:[],
        //表格选中数据(多选)，如果要回显选中数据可在 done 事件里面使用 setSelectedRowKeys 方法
        selection: [],
        // 表格列配置
        warehouseItemColumns: [
          {type:"selection",columnKey: 'selection',label: '选择', width: 50, align: 'center',fixed: 'left'},
          {prop: 'outOrderNo', label: '出库单号', showOverflowTooltip: true, align: 'center',},
          {prop: 'customerName', label: '客户名称',  showOverflowTooltip: true, align: 'center',},
          {prop: 'commissionAmountTotal', label: '提成金额', showOverflowTooltip: true,align: 'center',},
          {prop: 'currencyDesc', label: '币种',showOverflowTooltip: true,align: 'center',},
          {prop: 'commissionAmountCnTotal', label: '提成金额(￥)',   showOverflowTooltip: true, align: 'center',},
          {
            prop: 'createDate', label: '日期', showOverflowTooltip: true, align: 'center',sortable: 'createDate',
            formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
          },
        ],
      };
    },
    computed: {
      // 表格高度
      tableHeight() {
        var fh = this.fixedHeight ? 'calc(100vh - 350px)' : void 0;
        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 250px)' : null;
        return fh;
      },
    },
    methods: {

      /* 表格数据源 */
      async datasource(params) {

        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions) params.where.conditions = [];

        if(this.form.customerId){
          params.where.conditions.push({
            key:"customerId",
            operator: 0,
            value: this.form.customerId,
          });
        }

        if(this.form.currencyId){
          params.where.conditions.push({
            key:"currencyId",
            operator: 0,
            value: this.form.currencyId,
          });
        }
        
        //已出库
        params.where.conditions.push({
          key:"outStatus",
          operator: 0,
          value:  5,
        });
        
        //待核销
        params.where.conditions.push({
          key:"commissionStatus",
          operator: 10,
          value:  5,
        });
        
        // //未结清
        // params.where.conditions.push({
        //   key:"status",
        //   operator: 0,
        //   value:  1,
        // });

        let data = await QuerySaleoutorderByCondition(params.where);
       
        this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
        return {
            list:data.items,
            count:data.totalrecords,
        };
      },

      save(){
        if(this.selectRows.length==0){
          this.$message({
            message: '没有选择要核销的数据',
            type: 'warning'
          });
        }else{

          let outOrder = [];

          this.selectRows.map((item)=>{
            outOrder.push(item);
          })

          this.$emit('callback', outOrder);
          this.$emit('update:visible', false);
        }
      },

      //选择行数据
      handleSelectionChange(val) {
        this.selectRows = [];
        this.selectRows = val;
      },


      //单击单号
      clickbillNo(row){
        
        removePageTab({ key: '/businesshandling/saleoutmanager' });
        this.$nextTick(() => {
          this.$router.push({path: '/businesshandling/saleoutmanager', query: { "orderNo": row.orderNo } });
        });
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        //更新文件列表对像
      },

      handleClose(){
        this.$emit('update:visible', false);
      },

      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          //赋值this.form
          this.form = this.data;
          this.reload();
        } 
      }
    }
  };
  </script>